#include "RecognizerModule.h"
using namespace std;
/*******************************************************************/
CRecognizerModule::CRecognizerModule(void)
{
	m_featureExtractor = NULL; 
	m_txtWrite.SetColor("RED");
	m_index = 0; 
}
/*******************************************************************/
CRecognizerModule::~CRecognizerModule(void)
{
}
/*******************************************************************/
bool CRecognizerModule::LoadSVM( string path, string fname )
{
	return( m_classifier.Load(fname,path) ); 
}
/*******************************************************************/
IplImage * CRecognizerModule::DrawDetectedObjects( IplImage * img ) 
{
	IplImage * retVal = cvCreateImage(cvSize(img->width,img->height),img->depth, img->nChannels ); 
	cvCopy( img, retVal ); 
	CDataPointManager * data = NULL;
	data = m_extractor.GetAllObjectsInFrame(img);
	TRectVec rects = m_extractor.GetObjectCentroids();
	int sz = static_cast<int>(rects.size()); 
	for( int i=0; i < sz; i++ )
	{
		CvPoint a,b;
		CvRect rect = rects[i];
		a.x = rect.x;
		a.y = rect.y;
		b.x = rect.x+rect.width;
		b.y = rect.y+rect.height;
		cvDrawRect(img,a,b,CV_RGB(0,255,0));
	}
	if( NULL != data )
	{
		delete data;
	}
	return retVal; 
}
/*******************************************************************/
void CRecognizerModule::ExtractDetectedObjects( IplImage * img, string path, string fname, string ext)
{
	CDataPointManager * data = NULL;
	data = m_extractor.GetAllObjectsInFrame(img);
	TRectVec bb = m_extractor.GetObjectCentroids();
	for(int i=0; i < data->GetSize(); i++ )
	{
		stringstream ss;
		ss << path << fname << m_index << ext;
		cvSaveImage(ss.str().c_str(),data->GetDataPoint(i)->GetImage());
		cout << "Saved " << ss.str() << endl;
		m_index++;
	}
	if( NULL != data )
	{
		delete data;
	}

}
/*******************************************************************/
CFeatureExtractorBase * CRecognizerModule::GetFeatureExtractor( void )
{
	return m_featureExtractor;
}
/*******************************************************************/
void CRecognizerModule::SetFeatureExtractor( CFeatureExtractorBase* featExtractor )
{
	m_featureExtractor = featExtractor; 
	m_classifier.SetFeatureExtractor(m_featureExtractor);
}
/*******************************************************************/
SRecObjVec CRecognizerModule::RecognizeFrame(  IplImage * img )
{
	SRecObjVec retVal; 
	CDataPointManager * data = NULL;
	data = m_extractor.GetAllObjectsInFrame(img);
	m_classifier.Classify(data, false ); 
	TRectVec bb = m_extractor.GetObjectCentroids();
	for(int i=0; i < data->GetSize(); i++ )
	{
		CRecognizerObjects object; 
		object.SetStats( bb[i] );
		object.SetClassName( m_classifier.GetTheClassName(data->GetDataPoint(i)->GetClassification().GetExperiment())); 
		object.SetClassType(data->GetDataPoint(i)->GetClassification().GetExperiment());
		retVal.push_back(object); 
	}
	if( NULL != data )
	{
		delete data;
	}
	return retVal; 
}
/*******************************************************************/
CObjectExtractor& CRecognizerModule::GetObjectExtractor( void ) 
{
	return m_extractor;
}
/*******************************************************************/
IplImage* CRecognizerModule::WriteObjectsToFrame( IplImage * input, SRecObjVec& objects ) 
{
	int sz = static_cast<int>(objects.size()); 
	for( int i=0; i < sz; i++ )
	{
		CvPoint a,b;
		CvRect rect = objects[i].GetStats();
		a.x = rect.x;
		a.y = rect.y;
		b.x = rect.x+rect.width;
		b.y = rect.y+rect.height;
		cvDrawRect(input ,a,b,CV_RGB(0,255,0));
		string label = objects[i].GetClassName();
		m_txtWrite.WriteText(label,input,a);
	}
	return input;
}
/*******************************************************************/
CImgTxtWriter& CRecognizerModule::GetTextWritter( void )
{
	return m_txtWrite;
}
/*******************************************************************/
void CRecognizerModule::SetThreshold( int threshold )
{
	m_extractor.SetThreshold(threshold); 
	m_featureExtractor->SetThreshold(threshold); 
}
/*******************************************************************/
int CRecognizerModule::GetThreshold( void )
{
	return m_extractor.GetThreshold(); 
}
/*******************************************************************/
void CRecognizerModule::SetExtractorParams( int maxSz, int minSz, int minWH )
{
	m_extractor.SetMaxSize(maxSz);
	m_extractor.SetMinSize(minSz);
	m_extractor.SetMinWH(minWH);
}
/*******************************************************************/